package com.dhcc.android.base.utils.dataparse;

import android.util.Xml;

import com.dhcc.android.base.common.bean.Table;
import com.dhcc.android.base.common.bean.Task;

import org.xmlpull.v1.XmlPullParser;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 数据解析--调查期和表
 *
 * @author zhoudan
 * @date 2015年6月1日
 */
public class DataParse {

    @SuppressWarnings({"rawtypes", "unchecked"})
    public static List dataParse(InputStream is, Class<?> clazz, String startName) {
        List list = null;
        Object obj = null;

        XmlPullParser parser = Xml.newPullParser();

        try {
            parser.setInput(is, "utf-8");
            int eventType = parser.getEventType();

            while (eventType != XmlPullParser.END_DOCUMENT) {
                switch (eventType) {
                    case XmlPullParser.START_DOCUMENT:
                        list = new ArrayList();
                        break;
                    case XmlPullParser.START_TAG:
                        String name = parser.getName();

                        if (name.equalsIgnoreCase(startName)) {
                            obj = clazz.newInstance();
                            // 判断标签里是否有属性，如果有，则全部解析出来
                            int count = parser.getAttributeCount();
                            for (int i = 0; i < count; i++)
                                PullParse.setXmlValue(obj, parser.getAttributeName(i),
                                        parser.getAttributeValue(i));

                            list.add(obj);

                            obj = null;
                        }
                        break;
                    case XmlPullParser.END_TAG:

                        break;

                    default:
                        break;
                }

                eventType = parser.next();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;

    }

    public static List<Table> tableParse(InputStream is, String periodCode) {
        List<Table> list = null;
        Table table = null;
        XmlPullParser parser = Xml.newPullParser();

        try {
            parser.setInput(is, "utf-8");
            int eventType = parser.getEventType();

            while (eventType != XmlPullParser.END_DOCUMENT) {
                switch (eventType) {
                    case XmlPullParser.START_DOCUMENT:
                        list = new ArrayList<Table>();
                        break;
                    case XmlPullParser.START_TAG:
                        String name = parser.getName();

                        if ("TABLE".equalsIgnoreCase(name)) {
                            table = new Table();
                            // 判断标签里是否有属性，如果有，则全部解析出来
                            int count = parser.getAttributeCount();
                            for (int i = 0; i < count; i++)
                                PullParse.setXmlValue(table, parser.getAttributeName(i),
                                        parser.getAttributeValue(i));
                            if (table.getPeriod().contains(periodCode)) {
                                list.add(table);
                            }
                        }

                        break;
                    case XmlPullParser.END_TAG:

                        break;

                    default:
                        break;
                }

                eventType = parser.next();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return list;
    }


    public static Task parseTask(InputStream is) {
        Task task = null;
        XmlPullParser parser = Xml.newPullParser();

        try {
            parser.setInput(is, "utf-8");
            int eventType = parser.getEventType();

            while (eventType != XmlPullParser.END_DOCUMENT) {

                switch (eventType) {
                    case XmlPullParser.START_DOCUMENT:
                        task = new Task();
                        break;
                    case XmlPullParser.START_TAG:
                        // 判断标签里是否有属性，如果有，则全部解析出来
                        int count = parser.getAttributeCount();
                        for (int i = 0; i < count; i++)
                            PullParse.setXmlValue(task, parser.getAttributeName(i),
                                    parser.getAttributeValue(i));
                        break;
                    case XmlPullParser.END_TAG:

                        break;

                    default:
                        break;
                }

                eventType = parser.next();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return task;
    }

    public static Task parseTask(InputStream is, String period) {
        Task task = null;
        XmlPullParser parser = Xml.newPullParser();

        try {
            parser.setInput(is, "utf-8");
            int eventType = parser.getEventType();

            while (eventType != XmlPullParser.END_DOCUMENT) {

                switch (eventType) {
                    case XmlPullParser.START_DOCUMENT:
                        task = new Task();
                        break;
                    case XmlPullParser.START_TAG:
                        // 判断标签里是否有属性，如果有，则全部解析出来
                        int count = parser.getAttributeCount();
                        for (int i = 0; i < count; i++)
                            PullParse.setXmlValue(task, parser.getAttributeName(i),
                                    parser.getAttributeValue(i));
                        break;
                    case XmlPullParser.END_TAG:

                        break;

                    default:
                        break;
                }

                eventType = parser.next();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        if (period.equals(task.getStageids())) {
            return task;
        } else {
            return null;
        }
    }


    public static Task parseTask(InputStream is, String period, String table) {
        Task task = null;
        XmlPullParser parser = Xml.newPullParser();

        try {
            parser.setInput(is, "utf-8");
            int eventType = parser.getEventType();

            while (eventType != XmlPullParser.END_DOCUMENT) {

                switch (eventType) {
                    case XmlPullParser.START_DOCUMENT:
                        task = new Task();
                        break;
                    case XmlPullParser.START_TAG:
                        // 判断标签里是否有属性，如果有，则全部解析出来
                        int count = parser.getAttributeCount();
                        for (int i = 0; i < count; i++)
                            PullParse.setXmlValue(task, parser.getAttributeName(i),
                                    parser.getAttributeValue(i));
                        break;
                    case XmlPullParser.END_TAG:

                        break;

                    default:
                        break;
                }

                eventType = parser.next();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        if (period.equals(task.getStageids()) && table.equals(task.getTableids())) {
            return task;

        } else {
            return null;
        }
    }
}
